package com.qing.mvc.controller;

import com.qing.mvc.conver.DateEditor;
import com.qing.mvc.entity.User;
import com.qing.mvc.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserValidator userValidator;

//	在控制器初始化时注册属性编辑器
	@InitBinder
	public void initBinder(WebDataBinder binder){
		binder.registerCustomEditor(Date.class,new DateEditor());
	}

	@PostMapping("/login")
	@ResponseBody
	public void login(@RequestBody User user, Errors errors){
		userValidator.validate(user,errors);
		if(errors.hasErrors()){
			System.out.println(errors.getAllErrors().toString());
		}

		System.out.println(user.toString());
	}


}
